package com.lianxi.util;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;


/**
 * @author wh
 */
public class SecurityUtil {
    private static final Logger logger = LoggerFactory.getLogger(SecurityUtil.class);
    private static final Base64.Encoder ENCODER = Base64.getEncoder();
    private static final Base64.Decoder DECODER = Base64.getDecoder();

    public static void main(String[] args) {

       /* String url = "https://demo.henan.gov.cn/ywdt/hnyw/";
        System.out.println(StringUtil.getPath(url));*/

    }


    /**
     * 文件流转 byte 数组
     *
     * @param inStream
     * @return
     * @throws Exception
     */
    private static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        return outStream.toByteArray();
    }


    /**
     * 加密-base64
     *
     * @param str
     * @return
     */
    public static String base64Encode(String str) {
        String code = null;
        try {
            code = ENCODER.encodeToString(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            logger.info(str + " 加密失败");
            //加密失败返回原字符串
            code = str;
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 解密-base64
     *
     * @param str
     * @return
     */
    public static String base64Decode(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        str = str.replace("$", "+");
        str = str.replace("#", "/");
        byte[] code = null;
        try {
            code = DECODER.decode(str);
        } catch (Exception e) {
            logger.error(code + " base64解码异常：" + e.getMessage());
        }
        if (code != null) {
            try {
                return new String(code, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                logger.error(code + " base64解码异常UnsupportedEncodingException：" + e.getMessage());
            }
        }
        return null;
    }

    /**
     * MD5 加密
     *
     * @param password
     * @return
     */
    public static String md5(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            String md5str = new BigInteger(1, md.digest()).toString(16);
            return StringUtils.leftPad(md5str, 32, "0");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String md5(String username, String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(username.getBytes());
            md.update(password.getBytes());
            String md5str = new BigInteger(1, md.digest()).toString(16);
            return StringUtils.leftPad(md5str, 32, "0");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * SHA-1 加密
     *
     * @param value
     * @return
     */
    public static String hexSHA1(String value) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(value.getBytes("utf-8"));
            byte[] digest = md.digest();
            return byteToHexString(digest);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static String byteToHexString(byte[] bytes) {
        return String.valueOf(Hex.encodeHex(bytes));
    }

/*    *//**
     * url加密
     *
     * @param data
     * @return
     *//*
    public static String urlEncode(String data) {
        try {
            return URLEncoder.encode(data, "UTF-8");
        } catch (UnsupportedEncodingException e) {
//            throw new DaheException("urlEncode  is error ");
        }
    }

    *//**
     * URL解密
     *
     * @param data
     * @return
     *//*
    public static String urlDecode(String data) {
        try {
            return URLDecoder.decode(data, "UTF-8");
        } catch (UnsupportedEncodingException e) {
//            throw new DaheException("decodeurl  is error");
        }
    }*/


}
